package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.qualityevaluation.controller.VO.evaluatetaskVO;
import com.cqupt.qualityevaluation.controller.VO.evaluatetaskarrangeVO;
import com.cqupt.qualityevaluation.dao.EvaluateTaskArrangeMapper;
import com.cqupt.qualityevaluation.pojo.evaluatetask;
import com.cqupt.qualityevaluation.pojo.evaluatetaskarrange;
import com.cqupt.qualityevaluation.pojo.school;
import com.cqupt.qualityevaluation.pojo.schooltypecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("evaluatetaskarrange")
public class EvaluateTaskArrangeController {
    @Autowired
    private EvaluateTaskArrangeMapper evaluateTaskArrangeMapper;
    @GetMapping("/findAll")
    public List<evaluatetaskarrange> findAll(){
        return evaluateTaskArrangeMapper.selectList(null);
    }
    @GetMapping("/page")
    public evaluatetaskarrangeVO queryList(Integer current, Integer size){
        evaluatetaskarrangeVO evaluatetaskarrangeVO = new evaluatetaskarrangeVO();
        IPage<evaluatetaskarrange> page=new Page<>(current,size);
        evaluateTaskArrangeMapper.selectPage(page,null);
        evaluatetaskarrangeVO.setCurrent(current);
        evaluatetaskarrangeVO.setSize(size);
        evaluatetaskarrangeVO.setTotal(page.getTotal());
        evaluatetaskarrangeVO.setEvaluatetaskarrangeList(page.getRecords());
        return evaluatetaskarrangeVO;
    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody evaluatetaskarrange s){
        Map map=new HashMap<>();
        int result=0;
        try {
            result=evaluateTaskArrangeMapper.insert(s);
        }catch (Exception e){
            map.put("code","fail");
            map.put("msg","该学校不存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","发布成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id){
//        QueryWrapper<com.cqupt.qualityevaluation.pojo.schooltypecode> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("schooltypecode",schooltypecode);
        evaluateTaskArrangeMapper.deleteById(id);
        return "success";
    }
    @DeleteMapping("/delete/2/{schoolname}")
    public String deleteBySchoolname(@PathVariable("schoolname") String schoolname){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname",schoolname);
        evaluateTaskArrangeMapper.delete(queryWrapper);
        return "success";
    }
    @PutMapping("/update")
    public Object update(@RequestBody evaluatetaskarrange st){
        System.out.println("---------------"+st);
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<evaluatetaskarrange> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",st.getId());
        evaluatetaskarrange evaluatetaskarrange = new evaluatetaskarrange();
        evaluatetaskarrange.setEvaluatetaskid(st.getEvaluatetaskid());
        evaluatetaskarrange.setSchoolname(st.getSchoolname());
        evaluatetaskarrange.setIsfinished(st.getIsfinished());
        evaluatetaskarrange.setIsevaluated(st.getIsevaluated());
        try{
            result=evaluateTaskArrangeMapper.update(evaluatetaskarrange,updateWrapper);
        }
        catch (Exception e){
            map.put("code","fail");
            map.put("msg","该学校或该任务号不存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
    @GetMapping("/selectone/{id}")
    public evaluatetaskarrange findBySchoolType(@PathVariable("id") String id){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return evaluateTaskArrangeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/schooluser/{schoolname}")
    public evaluatetaskarrange selectBySchoolName(@PathVariable("schoolname") String schoolname){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname",schoolname);
        return evaluateTaskArrangeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/selectone/schooluser2/{schoolname}/{evaluatetaskid}")
    public evaluatetaskarrange selectBySchoolNameAnd(@PathVariable("schoolname") String schoolname,@PathVariable("evaluatetaskid") int evaluatetaskid){
//        System.out.println(schoolname);
//        System.out.println(evaluatetaskid);
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname",schoolname).eq("evaluatetaskid",evaluatetaskid);
        return evaluateTaskArrangeMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{evaluatetaskid}")
    public List<evaluatetaskarrange> findBySchoolTypeCode(@PathVariable("evaluatetaskid") String evaluatetaskid){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskid",evaluatetaskid);
        return evaluateTaskArrangeMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/11/{evaluatetaskid}")
    public List<evaluatetaskarrange> findBySchoolTypeCode2(@PathVariable("evaluatetaskid") String evaluatetaskid){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("evaluatetaskid",evaluatetaskid);
        return evaluateTaskArrangeMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{schoolname}")
    public List<evaluatetaskarrange> findBySchoolName(@PathVariable("schoolname") String schoolname){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("schoolname",schoolname);
        return evaluateTaskArrangeMapper.selectList(queryWrapper);
    }
    @GetMapping("/searchmorethanone/{evaluatetaskname}/{schoolname}/{isfinished}/{isevaluated}/{evaluateattitude}")
    public List<evaluatetaskarrange> findByMore(@PathVariable("evaluatetaskname") String evaluatetaskname,
                                   @PathVariable("schoolname") String schoolname,
                                   @PathVariable("isfinished") String isfinished,
                                   @PathVariable("isevaluated") String isevaluated,
                                   @PathVariable("evaluateattitude") String evaluateattitude){
        String newevaluatetaskname = null;
        String newschoolname=null;

        if (evaluatetaskname.equals("all")){
            newevaluatetaskname=evaluatetaskname.replaceAll("all","");
        }
        else {
            newevaluatetaskname=evaluatetaskname;
        }
        if (schoolname.equals("all")){
            newschoolname=schoolname.replaceAll("all","");
        }
        else {
            newschoolname=schoolname;
        }
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("evaluatetaskname",newevaluatetaskname)
                .like("schoolname",newschoolname)
                .like("isfinished",isfinished)
                .like("isevaluated",isevaluated)
                .like("evaluateattitude",evaluateattitude);
        return evaluateTaskArrangeMapper.selectList(queryWrapper);
    }
}
